//Demo
layui.use(['form','laydate','upload','layer',"element"], function(){
    var form = layui.form;
    var laydate = layui.laydate;
    var upload = layui.upload;
    var layer = layui.layer;
    var element = layui.element;


    $('#refresh').click(function (){
        location.reload();
    });

    $('#back').click(function (){
        layer.msg("请选择左侧导航栏进行返回")
    });




    <!-- 时间组件 -->
    laydate.render({
        elem: '#ks'
    });
    laydate.render({
        elem: '#js'
    });
    <!-- 上传视频 -->
    // var uploadInst = upload.render({
    //     elem: '#test1', //绑定元素
    //     accept:'video'
    //     ,size: 0
    //     ,url: '/img/uploadFile2' //上传接口
    //     ,done: function(res){
    //        layer.msg("上传成功")
    //     }
    //     ,error: function(){
    //         //请求异常回调
    //     }
    // });

    <!-- 上传图片 -->
    var uploadInst = upload.render({
        elem: '#upload1', //绑定元素
        url: '/YltUpload', //上传接口
        accept:'images'
        ,acceptMime:'image/jpg|image/png',
        before: function(obj){
            //预读本地文件示例
            obj.preview(function(index, files, result){
                $('#demo1').attr('src', result); //图片链接（base64）
            });
        },
        done: function(res){
            console.log(res.data.path);
            //如果上传失败
            if(res){
                //上传成功
                layer.alert("上传成功"+res.data.path);
                document.getElementById("pic").value = res.data.path;
                $("#imgDiv").css("display","")// 显示图片框
                $("input[name='carouselImg']").val(res.data.path)
            } else {
                return layer.msg('上传失败');
            }
        },
        error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();

            });
        }
    });




    <!-- 上传视频 -->
    var uploadInst = upload.render({
        elem: '#test2' //绑定元素
        ,accept: 'video'
        ,url: '/curriculum/intermediate/upload/' //上传接口
        ,before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
            layer.open({
                type: 0,
                title: false,
                closeBtn: 0,
                btn: false,
                content: '<div class="layui-progress layui-progress-big" lay-filter="progress" lay-showpercent="true"><div class="layui-progress-bar"></div></div>'
                ,success:function(layero,index){
                    var scanTime = 1000;
                    var timer = setInterval(function (){
                        $.ajax({
                            url: "/curriculum/intermediate/upload/setInter",
                            success: function (data) {
                                var arr = data.msg

                                var percent = arr;
                                element.progress('progress', percent +'%')
                                if(percent  == 100){
                                    $.ajax({
                                        url : "/curriculum/intermediate/upload/deleteVideo",
                                        type : 'post',
                                        success : function(res) {
                                            clearInterval(timer)
                                        },
                                        errorfun : function() {

                                        }
                                    });
                                    //关闭弹出层
                                    var index = parent.layer.getFrameIndex(window.name);
                                    layer.close(index);
                                    layer.msg('保存成功')
                                }

                            },
                            error: function (e) {
                                //关闭定时器
                                clearInterval(timer);
                                //关闭弹出层
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                layer.msg("保存失败");

                            }
                        });
                    }, scanTime);
                }
            });

        }
        ,done: function(res){
            console.log(res);
            console.log("1234214");
            var imgg = res.path;
            $("input[name=carouselVideo]").val(res.path);
            $("input[name=videoDuration]").val(res.h);
            $("video").attr("src",imgg);

        }
        ,error: function(){
            //请求异常回调
        }

    });





    //提交
    form.on('submit(formDemo)', function(data){

        //  layer.msg(JSON.stringify(data.field));
        $.ajax({
            type:'get',
            url:'/carousel/insert',
            data:data.field,
            dataType:'json',
            success:function (){

                console.log("进来了ajax")

                layer.msg('添加成功', {icon: 1,time:2000});
                //location.reload();
            },
            error:function (){
                console.log("错误了")
            }
        })
        window.setTimeout(function () {
            window.location.reload();
        },2000)
        console.log(data);
    });
});